import React, { Component } from 'react'
import { Tree } from 'antd'
const TreeNode = Tree.TreeNode

//treeData:[
//    {key:'a',title:'0',children:[
//        {key:'0-0-0',title:'0-0-0'},
//        {key:'0-0-1',title:'0-0-1'},
//    ]},
//    {key:'0-2',title:'0-2'}
//],

export default class TreeList extends Component {
  constructor(props) {
    super(props)
    this.state = {
      expandedKeys: ['000'],
      defaultExpandedKeys: ['000'],
    }
    this.handleonSelect = this.handleonSelect.bind(this)
  }

  componentDidMount() {
    // debugger
  }

  onExpand = (expandedKeys) => {
    this.setState({expandedKeys})
  }

  handleonSelect(info) {
    //expandedKeys
    console.log('selected', info)
    this.props.onSelect(info)
  }

  render() {
    const { trees } = this.props
    const { expandedKeys } = this.state
    const loop = data => data.map((item) => {
      if (item.children && item.children.length) {
        return <TreeNode key={item.deptcode} title={item.deptname}>{loop(item.children)}</TreeNode>
      }
      return <TreeNode key={item.deptcode} title={item.deptname}/>
    })
    const treeNodes = loop(trees)
    //expandedKeys={this.state.expandedKeys}
    return (
      <div>
        <Tree
          onSelect={this.handleonSelect}
          onExpand={this.onExpand}
          //expandedKeys={expandedKeys}
          defaultExpandedKeys={this.state.defaultExpandedKeys}
        >
          {treeNodes}
        </Tree>
      </div>
    )
  }
}
